package com.vtuzx.kingdee.xqc_api.service.impl;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxPage;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcHistoryService;
import com.vtuzx.kingdee.xqc_core.dao.IntegrationHistoryDao;
import com.vtuzx.kingdee.xqc_core.dao.InterfaceHistoryDao;
import com.vtuzx.kingdee.xqc_core.dao.OperationHistoryDao;
import com.vtuzx.kingdee.xqc_core.service.IVtuzxCoreService;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class XqcHistoryServiceImpl implements IXqcHistoryService {

    /** API服务 */
    @Autowired
    private IVtuzxCoreService coreService;

    /** API服务 */
    @Autowired
    private InterfaceHistoryDao marketHistoryDao;

    /** API服务 */
    @Autowired
    private OperationHistoryDao operationHistoryDao;

    /** API服务 */
    @Autowired
    private IntegrationHistoryDao integrationHistoryDao;

    @Override
    public VtuzxMap getHistoryList(VtuzxToken token, Map<String, Object> param) {
        String event = VtuzxUtil.getString(param, "event");
        String successStr = VtuzxUtil.getString(param, "success");
        boolean success = Boolean.parseBoolean(successStr);
        List<String> createTime = VtuzxUtil.getObject(param, "createTime");
        List<String> lastRunTime = VtuzxUtil.getObject(param, "lastRunTime");
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);

        Criteria cond = new Criteria();
        if (!VtuzxUtil.isEmpty(event)) {
            cond.and("event").is(event);
        }
        if (!VtuzxUtil.isEmpty(successStr)) {
            cond.and("success").is(success);
        }
        if (!VtuzxUtil.isEmpty(createTime)) {
            cond.and("createTime").gte(VtuzxUtil.parseDate(createTime.get(0))).lte(VtuzxUtil.parseDate(createTime.get(1)));
        }
        if (!VtuzxUtil.isEmpty(lastRunTime)) {
            cond.and("lastRunTime").gte(VtuzxUtil.parseDate(lastRunTime.get(0))).lte(VtuzxUtil.parseDate(lastRunTime.get(1)));
        }
        VtuzxPage page = integrationHistoryDao.page(cond, new Document("createTime", -1), pageIndex, pageSize);
        List<VtuzxMap> dataList = page.iterator(doc -> new VtuzxMap("id", VtuzxUtil.getString(doc, IVtuzxConst.Key__id))
                .append("event", VtuzxUtil.getString(doc, "event"))
                .append("success", VtuzxUtil.getBoolean(doc, "success"))
                .append("data", VtuzxUtil.json2Str(VtuzxUtil.getObject(doc, "data")))
                .append("retry", VtuzxUtil.getInt(doc, "retry"))
                .append("message", VtuzxUtil.getString(doc, "message"))
                .append("lastRunTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "lastRunTime")))
                .append("createTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "createTime")))
        );
        return new VtuzxMap(IVtuzxConst.Key_total, page.total()).append(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public VtuzxMap getOperationHistoryList(VtuzxToken token, Map<String, Object> param) {
        String userName = VtuzxUtil.getString(param, "userName");
        String typeName = VtuzxUtil.getString(param, "typeName");
        String content = VtuzxUtil.getString(param, "content");
        List<String> createTime = VtuzxUtil.getObject(param, "createTime");
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        Criteria cond = new Criteria();
        if (!VtuzxUtil.isEmpty(userName)) {
            cond.and("userName").regex(userName);
        }
        if (!VtuzxUtil.isEmpty(typeName)) {
            cond.and("typeName").regex(typeName);
        }
        if (!VtuzxUtil.isEmpty(content)) {
            cond.and("content").regex(content);
        }
        if (!VtuzxUtil.isEmpty(createTime)) {
            cond.and("createTime").gte(VtuzxUtil.parseDate(createTime.get(0))).lte(VtuzxUtil.parseDate(createTime.get(1)));
        }
        VtuzxPage page = operationHistoryDao.page(cond, new Document("createTime", -1), pageIndex, pageSize);
        List<VtuzxMap> dataList = page.iterator(doc -> new VtuzxMap("id", VtuzxUtil.getString(doc, IVtuzxConst.Key__id))
                .append("userName", VtuzxUtil.getString(doc, "userName"))
                .append("type", VtuzxUtil.getString(doc, "type"))
                .append("typeName", VtuzxUtil.getString(doc, "typeName"))
                .append("billNo", VtuzxUtil.getString(doc, "billNo"))
                .append("content", VtuzxUtil.getString(doc, "content"))
                .append("createTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "createTime")))
        );
        return new VtuzxMap(IVtuzxConst.Key_total, page.total()).append(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public void retry(VtuzxToken token, String historyId) throws Exception {
        Document history = integrationHistoryDao.findById(historyId);
        String event = VtuzxUtil.getString(history, "event");
        boolean success = VtuzxUtil.getBoolean(history, "success");
        if (success) {
            throw new VtuzxException("履历处理成功，不需要重试");
        }
        if ("returnStock".equals(event)) {
            coreService.syncReturnProductHistory(history);
        } else if ("outStock".equals(event)) {
            coreService.syncSaleOutStockHistory(history);
        }
    }
}
